/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.View;

import com.lcfgly.CustomBean.User;
import com.lcfgly.Dao.CountyImpl;
import com.lcfgly.Database.ShopArchives;
import com.lcfgly.Exception.ActionException;
import com.lcfgly.Service.ArchivesService;
import com.lcfgly.Service.MessageService;
import com.lcfgly.Service.OnepageService;
import com.lcfgly.Service.PriceService;
import com.lcfgly.Service.UserMenuService;
import com.lcfgly.Service.UserService;
import com.lcfgly.util.ConfigDate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 前台页面控制
 * @author lcfgly
 */
@Controller
public class index extends FatherController{
    
    @Autowired
    UserService userService;
    
    @Autowired
    UserMenuService usermenuService;
    
    @Autowired
    CountyImpl countyImpl;
    
    @Autowired
    PriceService priceService;
    
    @Autowired
    MessageService messageService;
    
    @Autowired
    ArchivesService archiveService;
    
    @Autowired
    OnepageService onepageService;
    
    /**
     * 店铺首页
     * @param username
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value = "/shop/{shopname}",method = RequestMethod.GET)
    public ModelAndView indexview(@PathVariable("shopname") String username)throws ActionException{
        ModelAndView result=new ModelAndView("default");
        User user=userService.findByUsername(username);
        result.addObject("userinfo", user);
        if(user.getUserinfer().getCityaddress()== null && user.getUserinfer().getCityid() != 0)
            result.addObject("useraddress",countyImpl.GetAddress(user.getUserinfer().getCityid()));
        else
            result.addObject("useraddress",user.getUserinfer().getCityaddress());
        result.addObject("product",priceService.indexProductList(user.getUsers().getUid()));
        result.addObject("message", messageService.indexMessage(user.getUsers().getUid()));
        result.addObject("headercss", "首页");
        return result;
    }
    
    /**
     * 栏目列表
     * @param id
     * @return 
     */
    @RequestMapping(value = "/header/{id}",method = RequestMethod.GET)
    public ModelAndView menu(@PathVariable("id") int id){
        ModelAndView view=new ModelAndView("header");
        try{
            getuser(view, id);
        }catch(ActionException e){
            //不注入user
        }
        view.addObject("menu", usermenuService.GetSimpleUserMenu(id));
        return view;
    }
    
    @RequestMapping(value = "/map/{shopid}")
    public ModelAndView map(@PathVariable("shopid") int shopid)throws ActionException{
        ModelAndView result=new ModelAndView("map");
        getuser(result, shopid);
        result.addObject("headercss", "地图");
        return result;
    }
    
    /**
     * 产品报价列表
     * @param shopid
     * @param page
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value = "/shoplist/{shopid}/{page}",method = RequestMethod.GET)
    public ModelAndView productlist(@PathVariable("shopid") int shopid,@PathVariable("page") int page)throws ActionException{
        ModelAndView result=new ModelAndView("product");
        getuser(result, shopid);
        result.addObject("list", priceService.productlist(shopid,page));
        result.addObject("typelist", usermenuService.GetProductType());
        result.addObject("headercss", "商品报价");
        return result;
    }
    
    /**
     * 产品报价列表
     * @param shopid
     * @param typeid
     * @param page
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value = "/shoplist/{shopid}/{typeid}/{page}",method = RequestMethod.GET)
    public ModelAndView productlist(@PathVariable("shopid") int shopid,@PathVariable("typeid") int typeid,@PathVariable("page") int page)throws ActionException{
        ModelAndView result=new ModelAndView("product");
        getuser(result, shopid);
        result.addObject("list", priceService.productlist(shopid, typeid, page));
        result.addObject("typelist", usermenuService.GetProductType());
        result.addObject("headercss", "商品报价");
         result.addObject("nowitem",typeid+"/");
        return result;
    }
    
    /**
     * 新闻列表
     * @param shopid
     * @param news
     * @param page
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value = "/shopnews/{shopid}/{news}/{page}",method = RequestMethod.GET)
    public ModelAndView newslist(@PathVariable("shopid") int shopid,@PathVariable("news") int news,@PathVariable("page") int page)throws ActionException{
        ModelAndView result=new ModelAndView("arclist");
        getuser(result, shopid);
        Map menuinfo=usermenuService.FindSimpleByIdAndUserId(shopid, news);
        result.addObject("list", archiveService.GetArchivesForPage(shopid, news, page));
        result.addObject("headercss", menuinfo.get("typename"));
        result.addObject("nowitem",news);
        return result;
    }
    
    /**
     * 新闻页面显示
     * @param shopid
     * @param id
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value = "/shopnewshow/{shopid}/{id}",method = RequestMethod.GET)
    public ModelAndView newshow(@PathVariable("shopid") int shopid,@PathVariable("id") int id)throws ActionException{
        ModelAndView result=new ModelAndView("newshow");
        getuser(result, shopid);
        ShopArchives arc=archiveService.findByIdAndUserid(id, shopid);
        result.addObject("info", arc);
        Map menuinfo=usermenuService.FindSimpleByIdAndUserId(shopid, arc.getTypeid());
        result.addObject("typeinfo", menuinfo);
        result.addObject("headercss", menuinfo.get("typename"));
        return result;
    }
    
    /**
     * 添加用户信息到返回视图
     * @param arg
     * @param shopid
     * @throws ActionException 
     */
    private void getuser(ModelAndView arg,int shopid)throws ActionException{
        User user = userService.findById(shopid);
        arg.addObject("userinfo", user);
        if (user.getUserinfer().getCityaddress() == null && user.getUserinfer().getCityid() != 0) {
            arg.addObject("useraddress", countyImpl.GetAddress(user.getUserinfer().getCityid()));
        }else{
            arg.addObject("useraddress", user.getUserinfer().getCityaddress());
        }
    }
    
    /**
     * 单页文档
     * @param shopid
     * @param id
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value = "/shopone/{shopid}/{id}",method = RequestMethod.GET)
    public ModelAndView onepage(@PathVariable("shopid") int shopid,@PathVariable("id") int id)throws ActionException{
        ModelAndView result=new ModelAndView("onepage");
        getuser(result, shopid);
        result.addObject("info", onepageService.findById(id));
        Map menuinfo=usermenuService.FindSimpleByIdAndUserId(shopid, id);
        result.addObject("headercss", menuinfo.get("typename"));
        return result;
    }
    
    /**
     * 留言列表页
     * @param shopid
     * @param page
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value = "/shopmessage/{shopid}/{page}",method = RequestMethod.GET)
    public ModelAndView message(@PathVariable("shopid") int shopid,@PathVariable("page") int page)throws ActionException{
        ModelAndView result=new ModelAndView("message");
        getuser(result, shopid);
        result.addObject("info", messageService.MessageList(shopid, page));
        result.addObject("headercss", "留言");
        return result;
    }
    
    /**
     * 添加回复
     * @param request
     * @return 
     */
    @RequestMapping(value = "/shopmessage/add",method = RequestMethod.POST)
    @ResponseBody
    public Map Messageadd(HttpServletRequest request){
        Map result=new HashMap();
         if(!request.getParameter("vcode").equalsIgnoreCase(request.getSession(true).getAttribute("vcode").toString())){
             result.put("success", false);
             result.put("msg", "验证码错误");
             return result;
         }
        Integer comeid=(Integer)request.getSession().getAttribute("id");
        Integer goid=Integer.valueOf(request.getParameter("go"));
        String message=request.getParameter("msg");
        if(comeid == null)
            comeid=0;
        com.lcfgly.Database.Message msg=new com.lcfgly.Database.Message();
        msg.setComeid(comeid);
        msg.setOpend(false);
        msg.setUptime(new Date());
        msg.setGoid(goid);
        msg.setMessage(message);
        try{
            messageService.create(msg);
            result.put("success", true);
        }catch(ActionException e){
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }
    
    /**
     * 跳转到商品详情
     * @param producttype
     * @param productid
     * @param resp
     */
    @RequestMapping("/product/{type}/{id}")
    public void productshow(@PathVariable("type") String producttype,@PathVariable("id") int productid,HttpServletResponse resp){
        StringBuilder url=new StringBuilder(ConfigDate.config("mainproductweb")+producttype+"/"+productid+".html");
        resp.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
        resp.setHeader("Location", url.toString());
    }
    
}
